package com.ruoyi.order.controller;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.order.domain.BalanceRechargeOrder;
import com.ruoyi.order.service.IBalanceRechargeOrderService;
import com.ruoyi.userManage.mapper.UserMapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 充值订单Controller
 *
 * @author ruoyi
 * @date 2025-08-13
 */
@RestController
@RequestMapping("/balanceRechargeOrder/balanceRechargeOrder")
public class BalanceRechargeOrderController extends BaseController {
    @Autowired
    private IBalanceRechargeOrderService balanceRechargeOrderService;
    @Resource
    private UserMapper userMapper;

    /**
     * 查询充值订单列表
     */
    @PreAuthorize("@ss.hasPermi('balanceRechargeOrder:balanceRechargeOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(BalanceRechargeOrder balanceRechargeOrder) {
        if (!StrUtil.isEmpty(balanceRechargeOrder.getUserPhone())) {
            Long userId = userMapper.selectIdByPhone(balanceRechargeOrder.getUserPhone());
            if (userId != null) {
                balanceRechargeOrder.setUserId(userId);
            } else {
                return getDataTable(Collections.emptyList());
            }
        }
        startPage();
        List<BalanceRechargeOrder> list = balanceRechargeOrderService.selectBalanceRechargeOrderList(balanceRechargeOrder);
        if (!CollectionUtils.isEmpty(list)) {
            for (BalanceRechargeOrder order : list) {
                String phone = userMapper.selectPhoneById(order.getUserId());
                order.setUserPhone(!StrUtil.isEmpty(phone) ? phone : "");
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出充值订单列表
     */
    @PreAuthorize("@ss.hasPermi('balanceRechargeOrder:balanceRechargeOrder:export')")
    @Log(title = "充值订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BalanceRechargeOrder balanceRechargeOrder) {
        if (!StrUtil.isEmpty(balanceRechargeOrder.getUserPhone())) {
            Long userId = userMapper.selectIdByPhone(balanceRechargeOrder.getUserPhone());
            if (userId != null) {
                balanceRechargeOrder.setUserId(userId);
            }
        }
        List<BalanceRechargeOrder> list = balanceRechargeOrderService.selectBalanceRechargeOrderList(balanceRechargeOrder);
        if (!CollectionUtils.isEmpty(list)) {
            for (BalanceRechargeOrder order : list) {
                String phone = userMapper.selectPhoneById(order.getUserId());
                order.setUserPhone(!StrUtil.isEmpty(phone) ? phone : "");
            }
        }
        ExcelUtil<BalanceRechargeOrder> util = new ExcelUtil<BalanceRechargeOrder>(BalanceRechargeOrder.class);
        util.exportExcel(response, list, "充值订单数据");
    }

    /**
     * 获取充值订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('balanceRechargeOrder:balanceRechargeOrder:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(balanceRechargeOrderService.selectBalanceRechargeOrderById(id));
    }

    /**
     * 新增充值订单
     */
    @PreAuthorize("@ss.hasPermi('balanceRechargeOrder:balanceRechargeOrder:add')")
    @Log(title = "充值订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BalanceRechargeOrder balanceRechargeOrder) {
        return toAjax(balanceRechargeOrderService.insertBalanceRechargeOrder(balanceRechargeOrder));
    }

    /**
     * 修改充值订单
     */
    @PreAuthorize("@ss.hasPermi('balanceRechargeOrder:balanceRechargeOrder:edit')")
    @Log(title = "充值订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BalanceRechargeOrder balanceRechargeOrder) {
        return toAjax(balanceRechargeOrderService.updateBalanceRechargeOrder(balanceRechargeOrder));
    }

    /**
     * 删除充值订单
     */
    @PreAuthorize("@ss.hasPermi('balanceRechargeOrder:balanceRechargeOrder:remove')")
    @Log(title = "充值订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(balanceRechargeOrderService.deleteBalanceRechargeOrderByIds(ids));
    }
}
